package ch.unifr.dokpe.htmlpicker.urlformater;

import java.util.Properties;
import java.lang.reflect.*;
/**
 * Create urlFormater object using the property urlFormater from the property file
 *  
 * The formating objects are create once, and are reused when needed
 * <pre>
 * UrlFormaterFactory fact = new UrlFormaterFactory(
 *	defaultProps.getProperty("urlFormater"), 
 * 	defaultProps.getProperty("url_param", "#"), 
 *	defaultProps.getProperty("url_count","@"), 
 *	defaultProps.getProperty("url_connect", ""));
 *</pre>		
 * Each property (named url_0, url_1 ... in the file) format an url segment 
 * The last url_i is used as default if the number of classes is bigger then the number of segments
 * @see ch.unifr.dokpe.htmlpicker.UrlManager.UrlBuilder#buildUrl()
 */
public class UrlFormaterFactory {

 	private AbstractUrlFormater[] formaters;
	private Properties properties;
	
/**
 * Create the factory object with the property urlFormater 
 * @param allClassNames String urlFormater property
 * @param argsymb String the url_param property
 * @param count_symb String url_count property
 * @param connect String url_connect property
 *
 */
	public UrlFormaterFactory(String allClassNames, String argsymb, String count_symb, String connect){
		initFactory(allClassNames,argsymb, count_symb, connect);
	}

	public UrlFormaterFactory(Properties properties) {
		this.properties = properties;
		initFactory(properties.getProperty("urlFormater"), properties.getProperty("url_param"), properties.getProperty("url_count"), properties.getProperty("url_connect") );
	}

	/**
	 * @param pos int position given by url_i in the properties
	 * @return Abstract Formater object for the url segment at the corresponding position, the last beeing returned if the number of factories exceed the number of properties
	 */
	public AbstractUrlFormater getFormater(int pos){
		int last = formaters.length-1;
		if (pos>last)
			pos=last;
		return formaters[pos];
	}

	/**
	 * @param allClassNames String a ; separated values of class names
	 * @param argsymb String the character that is used in the property file that will be replace with a value found in the parameters file
	 * @param count_symb String the character used in the property file that will be replace by a counter value
	 * @param connect String the string that is place between earch url segments
	 *
	 */

	private void initFactory(String allClassNames, String argsymb, String count_symb, String connect){
		String [] clNames = allClassNames.split(";\\s*");
		formaters = new AbstractUrlFormater[clNames.length];
		AbstractUrlFormater f = null;
		for (int i=0; i<clNames.length; i++){
			String clName;	
			String argName;
			try {
			          Class classDefinition; // = Class.forName(clNames[i]);
				  Constructor con;
				  
				  int pos = clNames[i].indexOf("(");
				  if (pos>0) {
					  clName = clNames[i].substring(0,pos);
					  classDefinition = Class.forName(clName);
					  argName = clNames[i].substring(pos+1, clNames[i].length()-1);
					try {	  
					  con = classDefinition.getConstructor(String.class);
					  f = (AbstractUrlFormater)con.newInstance(argName);
					} catch (InvocationTargetException e){e.printStackTrace();}
					  catch (NoSuchMethodException e){e.printStackTrace();}

					  if (f instanceof StaticUrlFormater){
						StaticUrlFormater sf = (StaticUrlFormater)f;
						sf.setProperties(properties);
					  }
  					  
				  } else {
					  
					  clName = clNames[i];
					  classDefinition = Class.forName(clName);
					  f = (AbstractUrlFormater) classDefinition.newInstance();

				  }
			          //f = (AbstractUrlFormater)classDefinition.newInstance();
				  //ajouter le cas de StaticUrlFormater ...
		      } 
		      	catch (InstantiationException e) {e.printStackTrace();} 
			catch (IllegalAccessException e) {e.printStackTrace();} 
			catch (ClassNotFoundException e) {e.printStackTrace();}
			
		f.setConnectString(connect);
		f.setParam_symb(argsymb);
		f.setCount_symb(count_symb);
		formaters[i] = f;
		
		}//for
	}

}//class

//@MITLI@
